package com.zdar.screens;

import java.util.ArrayList;


import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener;
import com.zdar.Zdar;
import com.zdar.entities.*;
import com.zdar.processing.XmlHandler;

public class GameScreen implements Screen {
	
	final Zdar game;	
	OrthographicCamera camera;
	
	Stage stage;	
	Skin skin;
	SpriteBatch batch;
	
	
	ArrayList<UnitTemplate> unitList = new ArrayList<UnitTemplate>();
	
	public GameScreen(Zdar game){
		this.game = game;
		batch = new SpriteBatch();
		skin = new Skin();
		skin.add("default", new BitmapFont());
		
		// Set the stage!
		stage = new Stage();
		Gdx.input.setInputProcessor(stage);		

		// Create a Table!
		setTable();		
		
		
		// Load unit data!
		XmlHandler xmlHandler = new XmlHandler();
		
		// Load images!
		
		// Load sounds!
		
		// Load music! (setLooping(true))
		
		// Create camera!
		camera = new OrthographicCamera();
		camera.setToOrtho(false, 1280, 720);	//TODO move sizes somewhere else
	}
	
	// based on this example: https://github.com/libgdx/libgdx/blob/master/tests/gdx-tests/src/com/badlogic/gdx/tests/UISimpleTest.java#L68
	public void setTable(){
		Table table = new Table();
		table.setFillParent(true);
		stage.addActor(table);
		//Text button style...?
		
		final TextButton button = new TextButton("fuck you!", skin);
		button.addListener(new ChangeListener(){
			public void changed(ChangeEvent event, Actor actor) {
				System.out.println("It's been clicked! " + button.isChecked());
			}
			
		});
		
	}
	
	public void update(){
		// update everyone's information
	}
	
	@Override
	public void render(float delta) {
		// Clear the screen with some tasty dark blue
		Gdx.gl.glClearColor(0, 0, 0.2f, 1);
		Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
		stage.act();
		stage.draw();		
		
		// tell camera to get its shit together and update its matrices
		camera.update();
		
		// tell the sprite to render in coord system set by camera
		game.batch.setProjectionMatrix(camera.combined);
		
		// begin a new batch and draw things again
		game.batch.begin();
		game.font.draw(game.batch, "Hey look, text!", 0, 720);
		//game.batch.draw(someShit, some, where);
		game.batch.end();
		
		// process user input
		if (Gdx.input.isKeyPressed(Keys.A)){
			System.out.println("You pressed A!");
		}
		// I guess here is where we make the listener and stuff, I'm actually not too sure
		// Really all we want are mouse clicks for now
		// apparently that's what scene 2d is for, so hurrah.
		
		// do some update checks to see if anything has changed.
		update();	//or just do it all in there.

	}

	@Override
	public void resize(int width, int height) {
		stage.getViewport().update(width, height, true);
	}

	@Override
	public void show() {
		//play music!
	}

	@Override
	public void hide() {
	}

	@Override
	public void pause() {
	}

	@Override
	public void resume() {
	}

	@Override
	public void dispose() {
		//dipose images
		//dispose sounds
		// I guess that's all
		stage.dispose();
		skin.dispose();
	}

}
